package com.kfm.base.reflect;

import java.lang.reflect.Constructor;

public class Demo04 {

    public static void main(String[] args) {
        Singleton instance = Singleton.getInstance();
        System.out.println(instance);

        Class<Singleton> singletonClass = Singleton.class;

        try {
            Constructor<Singleton> declaredConstructor = singletonClass.getDeclaredConstructor();
            declaredConstructor.setAccessible(true);

            Singleton singleton = declaredConstructor.newInstance();
            System.out.println(singleton);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

class Singleton {

    private static final Singleton instance = new Singleton();
    private Singleton(){
        if (instance != null){
            throw new RuntimeException("单例模式正在被攻击");
        }
    }

    public static Singleton getInstance(){
        return instance;
    }
}
